package com.ruoyi.crop.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Crop;
import com.ruoyi.crop.service.CropService;
import com.ruoyi.crop.service.GrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * 农作物Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("crop")
public class CropController extends BaseController {
    @Autowired
    CropService cropService;
    @Autowired
    GrowService growService;
    /**
     * 获取农作物列表
     */
    @RequiresPermissions("crop:products:list")
    @GetMapping("/list")
    public TableDataInfo list(Crop crop)
    {
        startPage();
        List<Crop> list = cropService.selectCropList(crop);
        return getDataTable(list);
    }
    @GetMapping("/FeignList")
    public AjaxResult FeignList()
    {
        List<Crop> list = cropService.listByTypeId();
        return success(list);
    }



    /**
     * 获取农作物添加
     */
    @RequiresPermissions("crop:products:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Crop crop)
    {
        long l = System.currentTimeMillis();
        crop.setCropCode("NZW"+l);
        Long userId = SecurityUtils.getUserId();
        crop.setUserId(Math.toIntExact(userId));
        crop.setCreateBy(SecurityUtils.getUsername());
        crop.setCreateTime(new Date());
        return success(cropService.save(crop));
    }

    /**
     * 查询农作物详细
     */
    @RequiresPermissions("crop:products:query")
    @GetMapping(value = "/{cropId}")
    public AjaxResult getInfo(@PathVariable String cropId)
    {
        return success(cropService.getById(cropId));
    }

    /**
     * 修改农作物
     */
    @RequiresPermissions("crop:products:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody Crop crop)
    {
        crop.setUpdateBy(SecurityUtils.getUsername());
        crop.setUpdateTime(new Date());
        return toAjax(cropService.updateById(crop));
    }

    /**
     * 删除农作物
     */
    @RequiresPermissions("crop:products:remove")
    @DeleteMapping("/{cropIds}")
    public AjaxResult remove(@PathVariable String[] cropIds)
    {
        return toAjax(cropService.removeBatchByIds(Arrays.asList(cropIds)));
    }


}
